package com.flashflexpro.flexdirector.adapter.codec{
    import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
    import com.flashflexpro.flexdirector.model.CodeStringValue;

    import mx.automation.IAutomationObject;

    public class CodecArray extends CodecDefault{
        public function CodecArray( ele:ICodec ){
            super();
            _elementCodec = ele;
        }

        private var _elementCodec:ICodec;


        override public function encode( targetObj:Object, descProperty:DescPropertyAuto,
                                         relativeParent:IAutomationObject ):CodeStringValue{
            var rt:CodeStringValue = new CodeStringValue();
            rt.value = getMemberFromObject( targetObj, descProperty );
            rt.desc = descProperty.getDto();

            var eleArr:Array = rt.value as Array;
            if( eleArr == null ){
                rt = null;
            }
            else{
                rt = encodeArray( eleArr, descProperty, relativeParent );
            }
            return rt;
        }

        public function encodeArray( eleArr:Array, descProperty:DescPropertyAuto,
                                     relativeParent:IAutomationObject ):CodeStringValue{

            var rt:CodeStringValue = new CodeStringValue();
            var codeArr:Array = new Array();
            var stringArr:Array = new Array();
            for( var j:int = 0; j < eleArr.length; ++j ){
                var eleDesc:DescPropertyAuto = new DescPropertyAuto();
                eleDesc.name = String( j );
                eleDesc.forDescription = descProperty.forDescription;
                eleDesc.forVerification = descProperty.forVerification;
                eleDesc.asType = descProperty.asType;
                CONFIG::debugging{
                    if( descProperty.asType == null ){
                        trace( this + ".encode descProperty.asType is null!" );
                    }
                }
                eleDesc.codecName = descProperty.codecName;
                eleDesc.defaultValue = null;

                var eleCSV:CodeStringValue = _elementCodec.encode( eleArr, eleDesc, relativeParent );
                eleCSV.string.replace( /;/g, "&#x3b;" );
                codeArr.push( eleCSV.code );
                stringArr.push( eleCSV.string );
            }
            rt.string = stringArr.join( ";" );
            rt.code = "[" + codeArr.join( "," ) + "]";
            return rt;
        }


        override public function decode( obj:Object, value:Object, descProperty:DescPropertyAuto,
                                         relativeParent:IAutomationObject, inString:Boolean = false ):void{
            if( value != null && value.length != 0 ){
                //since we override obj when calling the sub-codec
                //make sure relativeParent is set
                var delegate:IAutomationObject = obj as IAutomationObject;
                if( relativeParent == null ){
                    relativeParent = delegate;
                }

                var arr:Array = value as Array;
                var arrObj:Array = [];

                //note if the array members have extra spaces this code
                //won't work, we would need to trim the members, but that could
                //be a bad thing if the spaces are significant, so if we need to
                //do this, then we should also entity encode spaces (which would be ugly)

                for( var i:int = 0; i < arr.length; ++i ){
                    var arrayPropertyDescriptor:DescPropertyAuto = new DescPropertyAuto();
                    arrayPropertyDescriptor.name = String( i );
                    arrayPropertyDescriptor.forDescription = descProperty.forDescription;
                    arrayPropertyDescriptor.forVerification = descProperty.forVerification;
                    arrayPropertyDescriptor.asType = descProperty.asType;
                    CONFIG::debugging{
                        if( descProperty.asType == null ){
                            throw new Error( this + ".decode descProperty.asType = null !" );
                        }
                    }
                    arrayPropertyDescriptor.codecName = descProperty.codecName;

                    if( inString ){
                        var eleStr:String = arr[i].replace( /&#x3B;/g, ";" );
                    }
                    _elementCodec.decode( arr[ i ], eleStr, arrayPropertyDescriptor, relativeParent, inString );
                    arrObj.push( arr[i] );
                }

                obj[descProperty.name] = arrObj;
            }
        }
    }
}